package logic

import (
	"context"
	"github.com/pkg/errors"
	"person/models/mysql"
	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 用户登录
func (l *UserLoginLogic) UserLogin(in *personrpc.UserLoginRequest) (*personrpc.UserLoginResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	if err := user.FindUserByMobile(mysql.DB, in.Mobile); err != nil {
		return nil, err
	}
	if user.ID == 0 {
		return nil, errors.New("用户不存在，登录失败")
	}

	if in.Password != user.Password {
		return nil, errors.New("密码错误，登录失败")
	}

	return &personrpc.UserLoginResponse{
		UserId: int64(user.ID),
	}, nil
}
